#include "FourPointsPrimitive.h"
#include "Application.h"

#include "VMRPlayerScrollView.h"
#include "MRect.h"
#include "VMRPlayer.h"
#include "DX.h"
#include "DXWindow.h"
#include "DXImage.h"
#include "DXImageView.h"
#include "FourPointsPrimitive.h"
#include "VMRPlayerViewController.h"
#include "DXButtonView.h"
#include "NCenter.h"

VMRPlayerScrollView::VMRPlayerScrollView( const MRect& rect, DXImage* imageBack, DXImage* imageScroll, VMRPlayer* player ) : View( rect ),
																														_imageBack(  imageBack ), 
																														_imageScroll(  imageScroll ), 
																														_player( player )
																					
{
	_alpha = 0;
	float butSize = 49.0f;
	_imageManager.add( TEXT( "controlData\\play.png" ), new DXImage( TEXT( "controlData\\play.png" ) ) );
	_imageManager.add( TEXT( "controlData\\play_LIGHT.png" ), new DXImage( TEXT( "controlData\\play_LIGHT.png" ) ) );
	_imageManager.add( TEXT( "controlData\\pause.png" ), new DXImage( TEXT( "controlData\\pause.png" ) ) );
	_imageManager.add( TEXT( "controlData\\pause_LIGHT.png" ), new DXImage( TEXT( "controlData\\pause_LIGHT.png" ) ) );
		
	// play
	_playButton = new DXButtonView( MRect( 
											//this->rect().x(), 
											//this->rect().y() + 35.0f, 
											250.0f,
											this->rect().y() + 67.0f, 
											2 * butSize, 
											2 * butSize ), 
											_imageManager.get( TEXT( "controlData\\play.png" ) ), 
											_imageManager.get( TEXT( "controlData\\play_LIGHT.png" ) ),
											TEXT( "playAction" ) );

	_playButton->pressAction = new Observer< VMRPlayerScrollView >( this, &VMRPlayerScrollView::play );
	
	addSubView( _playButton );

	// pause
	_pauseButton = new DXButtonView( MRect( 
											//this->rect().x(), 
											//this->rect().y() + 35.0f, 
											250.0f,
											this->rect().y() + 67.0f, 
											2 * butSize, 
											2 * butSize ), 
											_imageManager.get( TEXT( "controlData\\pause.png" ) ), 
											_imageManager.get( TEXT( "controlData\\pause_LIGHT.png" ) ),
											TEXT( "pauseAction" ) );
	
	_pauseButton->pressAction = new Observer< VMRPlayerScrollView >( this, &VMRPlayerScrollView::pause );

	_playButton->setVisible( false );
	_pauseButton->setVisible( false );

	addSubView( _pauseButton );
}

VMRPlayerScrollView::~VMRPlayerScrollView( )
{
	removeSubView( _pauseButton );
	removeSubView( _playButton );

	delete _pauseButton;
	delete _playButton;
}

void VMRPlayerScrollView::update( )
{
	float butSize = 49.0f;

	D3DXMATRIX mTransf;
	D3DXVECTOR2 scale, transl;

	MRect clientRect = window()->getClientRect();
	float kx = 2.0f / clientRect.width();
	float ky = 2.0f / clientRect.height();

	scale.x = ( rect().width() - butSize ) / clientRect.width();
	scale.y = ( rect().height() - 70.0f ) / clientRect.height();
	transl.x = ( rect().width() - butSize ) / 2.0f * kx - 1.0f + ( rect().x() + butSize ) * kx;
	transl.y = ( rect().height() - 70.0f ) / 2.0f * ky - 1.0f + ( rect().y() + 45.f )* ky;
	
	D3DXMatrixTransformation2D( &mTransf, 0, 0, &scale, 0, 0, &transl );
	
	DX::_()->device->SetTransform( D3DTS_WORLD, &mTransf );
	
	DX::_()->device->SetTexture( 0, _imageBack->texture() );
	
	DWORD value;

	DX::_()->device->GetTextureStageState( 0, D3DTSS_COLORARG1, &value );
	if ( value != D3DTA_TEXTURE ) DX::_()->device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	
	DX::_()->device->GetTextureStageState( 0, D3DTSS_COLOROP, &value );
	if ( value != D3DTOP_SELECTARG1 ) DX::_()->device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
	
	DX::_()->device->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
	DX::_()->device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
	DX::_()->device->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
	DX::_()->device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

	DX::_()->device->SetRenderState( D3DRS_TEXTUREFACTOR, _alpha << 24 );

	//DX::_()->device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR );
	
	DX::_()->device->SetFVF( LCVertexTexture::FORMAT );
	DX::_()->device->SetStreamSource( 0, FourPointsPrimitive::_()->buffer(), 0, sizeof( LCVertexTexture ) );
	DX::_()->device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	//
	/*MRect scrollRect( rect().x() + butSize, rect().y(), ( rect().width() - butSize ) * ( float )( ( double )_player->position() / ( double )_player->duration() ), rect().height() );

	scale.x = ( scrollRect.width() ) / clientRect.width();
	scale.y = ( scrollRect.height() - 70.0f ) / clientRect.height();
	transl.x = ( scrollRect.width() ) / 2.0f * kx - 1.0f + scrollRect.x() * kx;
	transl.y = ( scrollRect.height() - 70.0f ) / 2.0f * ky - 1.0f + ( scrollRect.y() + 45.0f ) * ky;*/
	
	scrollRect.setX( rect().x() + 451.0f );
	scrollRect.setY( rect().y() + 109.0f ); 
	scrollRect.setWidth( ( 1183.0f ) * ( float )( ( double )_player->position() / ( double )_player->duration() ) );
	scrollRect.setHeight( 17.0f );
	
	//MRect scrollRect( rect().x() + butSize, rect().y(), ( rect().width() - butSize ) * ( float )( ( double )_player->position() / ( double )_player->duration() ), rect().height() );

	scale.x = ( scrollRect.width() ) / clientRect.width();
	scale.y = ( scrollRect.height() ) / clientRect.height();
	transl.x = ( scrollRect.width() ) / 2.0f * kx - 1.0f + ( scrollRect.x() ) * kx;
	transl.y = ( scrollRect.height() ) / 2.0f * ky - 1.0f + ( scrollRect.y() )* ky;

	D3DXMatrixTransformation2D( &mTransf, 0, 0, &scale, 0, 0, &transl );
	
	DX::_()->device->SetTransform( D3DTS_WORLD, &mTransf );
	
	DX::_()->device->SetTexture( 0, _imageScroll->texture() );

	DX::_()->device->SetFVF( LCVertexTexture::FORMAT );
	DX::_()->device->SetStreamSource( 0, FourPointsPrimitive::_()->buffer(), 0, sizeof( LCVertexTexture ) );
	DX::_()->device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	DX::_()->device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
	DX::_()->device->SetRenderState( D3DRS_TEXTUREFACTOR, 0xff << 24 );

	DX::_()->device->SetRenderState( D3DRS_ALPHABLENDENABLE, false );

	View::update();
}

void VMRPlayerScrollView::leftMouseDown( const IEvent& event )
{
	// float butSize = 49.0f;
	// LONGLONG newPos = ( LONGLONG )( ( event.point.x() - ( rect().x() + butSize ) ) * _player->duration() / ( rect().width() - butSize ) );
	
	if ( scrollRect.x() < event.point.x() && ( scrollRect.x() + 1183.0f ) > event.point.x() )
	{
		LONGLONG newPos = ( LONGLONG )( ( event.point.x() - scrollRect.x() ) * _player->duration() / ( 1183.0f ) );

		if ( _delegate ) _delegate->scrollChangedPosition( ( void* )&newPos );
	
		if ( !_player->isVideoPlaying() )
		{
			_playButton->setVisible( false );
			_pauseButton->setVisible( true );
		}
	}
}

void VMRPlayerScrollView::track( bool isTrackOn  )
{
	if ( isTrackOn )
	{
		_alpha = 0xff;
	
		if ( _player->isVideoPlaying() ) 
		{
			_playButton->setVisible( false );
			_pauseButton->setVisible( true );
		}
		else
		{
			_playButton->setVisible( true );
			_pauseButton->setVisible( false );
		}
		setVisible( true );
	}
	else
	{
		_playButton->setVisible( false );
		_pauseButton->setVisible( false );
		
		_alpha = 0x00;
		setVisible( false );
	}
}

//bool VMRPlayerScrollView::rollOver( IEvent event )
//{
//	_alpha = 0xff;
//	
//	if ( _player->isVideoPlaying() ) 
//	{
//		_playButton->setVisible( false );
//		_pauseButton->setVisible( true );
//	}
//	else
//	{
//		_playButton->setVisible( true );
//		_pauseButton->setVisible( false );
//	}
//
//	return true;
//}
//
//bool VMRPlayerScrollView::rollOut( IEvent event )
//{
//	_alpha = 0;
//	_playButton->setVisible( false );
//	_pauseButton->setVisible( false );
//	return true;
//}

void VMRPlayerScrollView::delegate( VMRPlayerScrollViewDelegate* delegate )
{
	assert( delegate );
	_delegate = delegate;
}

VMRPlayerScrollViewDelegate* VMRPlayerScrollView::delegate() const
{
	return _delegate;
}

void VMRPlayerScrollView::play( IParam* param )
{
	if ( _delegate ) _delegate->playButtonPressed( 0 );
	_playButton->setVisible( false );
	_pauseButton->setVisible( true );
}

void VMRPlayerScrollView::pause( IParam* param )
{
	if ( _delegate ) _delegate->pauseButtonPressed( 0 );
	_playButton->setVisible( true );
	_pauseButton->setVisible( false );
}
//
//void VMRPlayerView::mouseMove( IEvent event )
//{
//	if ( event.lPressed )
//	{
//		_rect.x -= event.prevPoint.x - event.point.x;
//		_rect.y -= event.prevPoint.y - event.point.y;
//	}
//}
//
//void VMRPlayerView::leftMouseUp( IEvent event )
//{
//	_player->play();
//}